import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class Main {
    private static Manager manager;
    private static Scanner scanner;

    public static void main(String[] args) {
        manager = new Manager();
        scanner = new Scanner(System.in);

        while (true) {
            printMenu();
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();

                switch (choice) {
                    case 1:
                        addStudent();
                        break;
                    case 2:
                        deleteStudent();
                        break;
                    case 3:
                        updateStudent();
                        break;
                    case 4:
                        findStudentById();
                        break;
                    case 5:
                        findStudentsByName();
                        break;
                    case 6:
                        getAllStudents();
                        break;
                    case 7:
                        findStudentsByDormitory();
                        break;
                    case 8:
                        findDormLeaders();
                        break;
                    case 9:
                        System.out.println("退出系统，关闭连接...");
                        manager.close();
                        return;
                    default:
                        System.out.println("无效的选择，请重新输入。");
                }
            } catch (InputMismatchException e) {
                System.out.println("输入无效，请输入一个数字。");
                scanner.nextLine();
            }
        }
    }

    private static void printMenu() {
        System.out.println("\n学生宿舍管理系统菜单：");
        System.out.println("1. 添加学生");
        System.out.println("2. 删除学生");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 按学号查找学生");
        System.out.println("5. 按姓名查找学生");
        System.out.println("6. 显示所有学生");
        System.out.println("7. 按宿舍查找学生");
        System.out.println("8. 查找宿舍长");
        System.out.println("9. 退出系统");
        System.out.print("请输入你的选择：");
    }

    private static void addStudent() {
        System.out.print("请输入学号：");
        String studentId = scanner.nextLine();
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();
        System.out.print("请输入班级：");
        String className = scanner.nextLine();
        System.out.print("请输入宿舍：");
        String dormitory = scanner.nextLine();
        System.out.print("是否为宿舍长 (true/false)：");
        boolean isDormLeader = scanner.nextBoolean();
        scanner.nextLine();

        Students student = new Students(studentId, name, age, className, dormitory, isDormLeader);
        boolean result = manager.addStudent(student);
        System.out.println(result ? "学生添加成功。" : "学生添加失败。");
    }

    private static void deleteStudent() {
        System.out.print("请输入要删除的学生的学号：");
        String studentId = scanner.nextLine();
        boolean result = manager.deleteStudent(studentId);
        System.out.println(result ? "学生删除成功。" : "学生删除失败，未找到该学号的学生。");
    }

    private static void updateStudent() {
        System.out.print("请输入要更新信息的学生的学号：");
        String studentId = scanner.nextLine();
        System.out.print("请输入新的学号：");
        String newStudentId = scanner.nextLine();
        System.out.print("请输入新的姓名：");
        String newName = scanner.nextLine();
        System.out.print("请输入新的年龄：");
        int newAge = scanner.nextInt();
        scanner.nextLine();
        System.out.print("请输入新的班级：");
        String newClassName = scanner.nextLine();
        System.out.print("请输入新的宿舍：");
        String newDormitory = scanner.nextLine();
        System.out.print("是否为宿舍长 (true/false)：");
        boolean newIsDormLeader = scanner.nextBoolean();
        scanner.nextLine();

        Students newStudent = new Students(newStudentId, newName, newAge, newClassName, newDormitory, newIsDormLeader);
        boolean result = manager.updateStudent(studentId, newStudent);
        System.out.println(result ? "学生信息更新成功。" : "学生信息更新失败，未找到该学号的学生。");
    }

    private static void findStudentById() {
        System.out.print("请输入要查找的学生的学号：");
        String studentId = scanner.nextLine();
        Students student = manager.findStudentById(studentId);
        if (student != null) {
            List<Students> studentList = List.of(student);
            manager.showDatabaseView(studentList);
        } else {
            System.out.println("未找到该学号的学生。");
        }
    }

    private static void findStudentsByName() {
        System.out.print("请输入要查找的学生的姓名（支持模糊查询）：");
        String name = scanner.nextLine();
        List<Students> students = manager.findStudentsByName(name);
        if (!students.isEmpty()) {
            manager.showDatabaseView(students);
        } else {
            System.out.println("未找到符合条件的学生。");
        }
    }

    private static void getAllStudents() {
        List<Students> students = manager.getAllStudents("ASC", "student_id");
        manager.showDatabaseView(students);
    }

    private static void findStudentsByDormitory() {
        System.out.print("请输入要查找的宿舍号：");
        String dormitory = scanner.nextLine();
        List<Students> students = manager.findStudentsByDormitory(dormitory);
        if (!students.isEmpty()) {
            manager.showDatabaseView(students);
        } else {
            System.out.println("该宿舍未找到学生。");
        }
    }

    private static void findDormLeaders() {
        List<Students> dormLeaders = manager.findDormLeaders();
        if (!dormLeaders.isEmpty()) {
            manager.showDatabaseView(dormLeaders);
        } else {
            System.out.println("未找到宿舍长。");
        }
    }
}